package com.wlyuan.open.scanner.repository.impl;

import com.wlyuan.open.scanner.dao.mapper.BalanceBillDetailsMapper;
import com.wlyuan.open.scanner.domain.finance.BillCost;
import com.wlyuan.open.scanner.domain.finance.BillSpec;
import com.wlyuan.open.scanner.domain.finance.BillSpecConvert;
import com.wlyuan.open.scanner.repository.BalanceBillDetailRepository;
import com.wlyuan.open.scanner.utils.StreamUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BalanceBillDetailRepositoryImpl implements BalanceBillDetailRepository {
    private final BalanceBillDetailsMapper balanceBillDetailsMapper;

    @Override
    public List<BillSpec> getSpecById(Long id) {
        var list = balanceBillDetailsMapper.getList(id);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return BillSpecConvert.convert(list);
    }

    @Override
    public Map<Long, BillCost> getCostCloseMap() {
        var list = balanceBillDetailsMapper.getCostClose();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return StreamUtils.toMap(list, BillCost::getFinanceId);
    }

    @Override
    public Map<Long, BillCost> getCostMap() {
        var list = balanceBillDetailsMapper.getCost();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return StreamUtils.toMap(list, BillCost::getFinanceId);
    }

    @Override
    public Long getOrderCountByBillIndexIdClose(Long id) {
        return balanceBillDetailsMapper.getOrderCountByBillIndexIdClose(id);
    }

    @Override
    public Long getOrderCountByBillIndexId(Long id) {
        return balanceBillDetailsMapper.getOrderCountByBillIndexId(id);
    }

}
